﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Condimar.Entities;
using Condimar.BussinesLogic;

namespace Condimar.UI
{
    public partial class FrmRegistrarMarca : Form
    {
        [Dependency]
        public IMarcaBL marcaBL { get; set; }

        bool b;
        public FrmRegistrarMarca()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public Marca CurrentMarca { get; set; }
        public void Nuevo()
        {
            b = true;
            marcaBindingSource.AddNew();
        }

        public void Editar()
        {
            b = false;
            marcaBindingSource.DataSource = CurrentMarca;
        }
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            Marca currentMarca = marcaBindingSource.Current as Marca;



            if ((descripcionTextBox.Text).Length != 0)
            {
                if (currentMarca != null)
                {
                    if (b == true)
                    {
                                    var existemarca = (from m in marcaBL.GetAllFromMarcas()
                                          where m.Descripcion.CompareTo(descripcionTextBox.Text) == 0 && m.Id != currentMarca.Id
                                         select m).Any();

                        if (existemarca == false)
                        {
                            marcaBL.AddMarca(currentMarca);
                            MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                        else
                            {
                                MessageBox.Show("Ya esta registrado", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }   
                    }
                    if (b == false)
                    {
                            marcaBL.UpdateMarca(currentMarca);
                            MessageBox.Show("Modificacion Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                    }
                }
            else
                MessageBox.Show("Debe completar todos los campos", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void FrmRegistrarMarca_Load(object sender, EventArgs e)
        {

        }
    }
}
